<template>
  <el-dialog
      :title="title"
      custom-class="small-dialog"
      :visible="visible"
      append-to-body
      @open="open(),initTop()"
      @close="close"
  >
    <div class="search-pannel">
      <el-form
          class="search-form"
          :model="postData"
          :rules="postRules"
          ref="postForm"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="设备名称" prop="name">
              <el-input v-model="postData.name" placeholder="请输入特点名称" maxlength="20" show-word-limit></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col v-if="!this.opId" :span="24">
            <el-form-item label="设备类型" prop="parentId">
              <el-select v-model="postData.parentId">
                <el-option v-for="item in topList" :key="item.value" :value="item.value" :label="item.text"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { _post, _put, _getDetail, _getTop } from '../src/store'
import { getAllArea, postDialog } from '@/mixins'

export default {
  name: 'postDialog',
  mixins: [postDialog, getAllArea],
  data() {
    return {
      postData: {
        name: '',
        parentId: ''
      },
      postRules: {
        name: [{ required: true, message: '请输入特点名称', trigger: 'blur' }]
      },
      addFunc: _post,
      editFunc: _put,
      detailFunc: _getDetail,
      topList: []
    }
  },
  methods: {
    async initTop() {
      const { data, success, message } = await _getTop()
      this.topList = []
      if (success) {
        this.topList = data.map(item => ({ value: item.id, text: item.name }))
      } else {
        this.$message({ type: 'error', message })
      }
      this.topList.unshift({ value: '', text: '顶级' })
    }
  }
}
</script>

<style scoped>

</style>
